package test.spring.ioc.impl;

import test.spring.ioc.NewsBean;
import test.spring.ioc.NewsListener;
import test.spring.ioc.NewsProvider;
import my.spring.aop.annotation.Aop;
import my.spring.ioc.annotation.Bean;
import my.spring.ioc.annotation.Inject;
import test.spring.aop.MyAop;

/**
 * @author fengbo
 * @date 2018/6/21
 */
@Bean("newsProvider")
@Aop
public class NewsProviderImpl implements NewsProvider {

    @Inject("newsListenerImpl")
    private NewsListener newsListener;
    @Inject("newsBean")
    private NewsBean newsBean;

    public NewsProviderImpl() {
    }

    public NewsProviderImpl(NewsListener newsListener, NewsBean newsBean) {
        this.newsListener = newsListener;
        this.newsBean = newsBean;
    }

    @Override
    public void getAndPersistNews() {
        String[] newsIds = newsListener.getAvailableNewsIds();
        if(newsIds == null || newsIds.length == 0) {
            return;
        }
        for(String newsId : newsIds) {
            System.out.println(newsId);
        }
    }

    @Override
    @MyAop
    public void testAOP() {
        // 即使getAndPersistNews方法被SpringAOP拦截，这里也不会调用拦截器。
        // 解决办法参考Spring揭秘P243.
//        getAndPersistNews();
        System.out.println(newsBean);
    }
}
